package com.kye.express.business.customer_code.modify_customer_password;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.databinding.ActivityModifyCustomerPasswordBinding;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;

import java.util.Objects;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function3;

public class ModifyCustomerPasswordActivity extends BaseToolbarActivity implements ModifyCustomerPasswordContract.ModifyCustomerPasswordView {

    private ActivityModifyCustomerPasswordBinding mBinding;
    private ModifyCustomerPasswordContract.ModifyCustomerPasswordPresenter mPresenter;
    private Dialog mDialog;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, ModifyCustomerPasswordActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_modify_customer_password);

        initEvent();
        initData();
    }

    private void initEvent() {
        setUpToolbar(mBinding.toolbar);

        Observable.combineLatest(
                RxTextView.textChanges(mBinding.editTextOldPassword),
                RxTextView.textChanges(mBinding.editTextNewPasswrod),
                RxTextView.textChanges(mBinding.editTextConfirmPassword),
                new Function3<CharSequence, CharSequence, CharSequence, Boolean>() {
                    @Override
                    public Boolean apply(CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3) throws Exception {
                        return !TextUtils.isEmpty(charSequence) && !TextUtils.isEmpty(charSequence2) && !TextUtils.isEmpty(charSequence3);
                    }
                })
                .compose(this.<Boolean>bindToLifecycleDestroy())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        mBinding.buttonConfirm.setEnabled(aBoolean);
                    }
                });

        RxView.clicks(mBinding.buttonConfirm)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        String oldPassword = mBinding.editTextOldPassword.getText().toString().trim();
                        String newPassword = mBinding.editTextNewPasswrod.getText().toString().trim();
                        String confirmPassword = mBinding.editTextConfirmPassword.getText().toString().trim();

                        if (!Objects.equals(newPassword, confirmPassword)) {
                            ToastManager.show(ModifyCustomerPasswordActivity.this, R.string.modify_customer_two_time_password_not_identical);
                            return;
                        }
                        // TODO
                        mPresenter.modifyPassword(oldPassword, newPassword);
                    }
                });
    }

    private void initData() {
        mPresenter = new ModifyCustomerPasswordContract.ModifyCustomerPasswordPresenter();
        mPresenter.bind(this);
    }

    @Override
    public void onError() {
        ToastManager.show(this, R.string.common_net_error);
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg);
    }

    @Override
    public void onLoading() {
        mDialog = KyeProgressDialog.show(this, mDialog);
    }

    @Override
    public void onLoaded() {
        KyeProgressDialog.dismiss(mDialog);
    }

    @Override
    public void onSuccess() {
        finish();
    }
}
